/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.Message;
import org.crystalrange.enterprise.model.CompanyCode;
import org.crystalrange.enterprise.model.SalesOrganization;
import org.crystalrange.enterprise.service.SalesOrganizationService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

public class SalesOrganizationController extends BaseController {

    protected SalesOrganizationService salesOrganizationService;
    protected SalesOrganization salesOrganization;
    protected Listbox listboxCompanyCode;

    public SalesOrganizationService getSalesOrganizationService() {
        return this.salesOrganizationService;
    }

    public void setSalesOrganizationService(SalesOrganizationService salesOrganizationService) {
        this.salesOrganizationService = salesOrganizationService;
    }

    public SalesOrganization getSalesOrganization() {
        return this.salesOrganization;
    }

    public void setSalesOrganization(SalesOrganization salesOrganization) {
        this.salesOrganization = salesOrganization;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        if (this.execution.getParameter("Id") != null) {
            this.salesOrganization = this.salesOrganizationService.find(
                    new Long(this.execution.getParameter("Id")));
            this.labelTitle.setValue("Edit Sales Organization");
        } else {
            this.salesOrganization = this.salesOrganizationService.create(
                    SecurityContextHolder.getContext().getAuthentication().getName());
            this.labelTitle.setValue("Create Sales Organization");
        }

        component.setAttribute("controller", this);
    }

    public void onClick$menuitemCompanyCodeAdd(Event event) throws Exception {
        this.companyCodeAdd();
    }

    public void onClick$menuitemCompanyCodeRemove(Event event) throws Exception {
        this.companyCodeRemove();
    }

    public void onClick$toolbarbuttonCompanyCodeAdd(Event event) throws Exception {
        this.companyCodeAdd();
    }

    public void onClick$toolbarbuttonCompanyCodeRemove(Event event) throws Exception {
        this.companyCodeRemove();
    }

    public void onClick$menuitemSalesOrganizationSave(Event event) throws Exception {
        this.salesOrganizationSave();
    }

    public void onClick$toolbarbuttonSalesOrganizationSave(Event event) throws Exception {
        this.salesOrganizationSave();
    }

    private void companyCodeAdd() throws Exception {
        Window window = (Window) this.execution.createComponents("~./zul/html/companyCodeSelect.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            CompanyCode selectedCompanyCode = (CompanyCode) this.execution.getAttribute("selectedCompanyCode");
            this.salesOrganization.getCompanyCodes().add(selectedCompanyCode);
        }

        this.binder.loadComponent(this.listboxCompanyCode);
    }

    private void companyCodeRemove() throws Exception {
        Set<Listitem> selectedItems = this.listboxCompanyCode.getSelectedItems();

        for (Listitem selectedItem : selectedItems) {
            CompanyCode selectedCompanyCode = (CompanyCode) selectedItem.getValue();
            this.salesOrganization.getCompanyCodes().remove(selectedCompanyCode);
        }

        this.binder.loadComponent(this.listboxCompanyCode);
    }

    private void salesOrganizationSave() throws Exception {
        this.salesOrganizationService.save(this.salesOrganization,
                SecurityContextHolder.getContext().getAuthentication().getName());
        this.sessionScope.put("CRYSTALRANGE_MESSAGE", new Message("Saved successfully!"));
        Clients.evalJavaScript("window.history.back();");
    }
}
